﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.BaseObject;
using System.Data;

namespace Honestysoft.Financing.WebUI.Module.Project
{
    public partial class SelectSplitProject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AspNetPager1.PageSize = Globals.PageSize;
                BindControl();
                Bind();
            }
        }
        /// <summary>
        /// 绑定页面控件。
        /// </summary>
        private void BindControl()
        {
            ControlHelper.BindDictionary(ddlProjectCategory, "ProjectCategory");
            ControlHelper.InsertAllItemAndSelect(ref ddlProjectCategory);
        }
        /// <summary>
        /// 绑定列表
        /// </summary>
        private void Bind()
        {
            int pageCount = 0, recordCount = 0;
            DataTable dtList = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetSelectSplitProject(ddlProjectCategory.SelectedValue == "0" ? null : ddlProjectCategory.SelectedValue, txtCode.Text.Trim(), txtSN.Text.Trim(), txtName.Text.Trim(), AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out pageCount, out recordCount);
            AspNetPager1.RecordCount = recordCount;
            rptList.DataSource = dtList;
            rptList.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }
        /// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private string[] GetSelectedRowKeys()
        {
            string strId = Request["chkID"] + "";
            if (string.IsNullOrEmpty(strId)) { return null; }
            return strId.Split(',');
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string[] arrId = GetSelectedRowKeys();
            if (arrId == null)
            {
                ScriptHelper.Window.Alert("请选择项目后再执行此操作！");
                return;
            }
            Honestysoft.Financing.BusinessLogic.Project bll = new BusinessLogic.Project(Globals.CurrentConnStringName);
            bool bRet = false;
            if (string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                bRet= bll.CreateProjectSplit(arrId);                
            }
            else
            {
                bRet = bll.AddProjectSplit(arrId, Request.QueryString["ID"]);
            }
            if (bRet)
            {
                ScriptHelper.Window.AlertClose("设置拆分项目成功！", true);
            }
            else
            {
                ScriptHelper.Window.Alert("设置拆分项目失败！");
            }
        }
    }
}